package internationalenergytradingplatform.controller.Transporter;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import internationalenergytradingplatform.VO.OrderPageQueryVO;
import internationalenergytradingplatform.context.BaseContext;
import internationalenergytradingplatform.dao.AbnormalDao;
import internationalenergytradingplatform.dao.OrderMapper;
import internationalenergytradingplatform.dto.OrderPageQueryDTO;
import internationalenergytradingplatform.pojo.Abnormal;
import internationalenergytradingplatform.pojo.Order;
import internationalenergytradingplatform.result.PageResult;
import internationalenergytradingplatform.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/transporter-order")
public class TransporterOrderController {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    AbnormalDao abnormalDao;

    /**
     *
     * @author yingfeng
     * @date 2024/3/27 16:32
    查询当前账号已接订单
     */
    @GetMapping("/paginationQuery")
    public Result<PageResult> pagingToQueryOrderByTele(OrderPageQueryDTO orderPageQueryDTO){
        String companyId = BaseContext.getCurrentId();//获取公司id
        orderPageQueryDTO.setTransporter(companyId);
        PageHelper.startPage(orderPageQueryDTO.getPage(), orderPageQueryDTO.getPageSize());
        Page<OrderPageQueryVO> page = orderMapper.orderPageQueryProcess(orderPageQueryDTO);
        long total = page.getTotal();
        List<OrderPageQueryVO> records = page.getResult();
        log.info("分页查询结果：{}", records);
        PageResult pageResult = new PageResult(total, records);
        return Result.success(pageResult);
    }

    /**
     *
     * @author yingfeng
     * @date 2024/3/27 16:36
     查询当前账号可接订单
     */
    @GetMapping("/paginationQueryAvailableOrders")
    public Result<PageResult> pagingToQueryAvailableOrders(OrderPageQueryDTO orderPageQueryDTO){
        String companyId = BaseContext.getCurrentId();//获取公司id
        orderPageQueryDTO.setTransporter(companyId);
        PageHelper.startPage(orderPageQueryDTO.getPage(), orderPageQueryDTO.getPageSize());
        Page<OrderPageQueryVO> page = orderMapper.orderPageQueryAvailable(orderPageQueryDTO);
        long total = page.getTotal();
        List<OrderPageQueryVO> records = page.getResult();
        log.info("分页查询结果：{}", records);
        PageResult pageResult = new PageResult(total, records);
        return Result.success(pageResult);
    }

    /**
     * 将状态由运输商运输中（2）改为待采购商收货（3）或者异常（5）
     * 如果是收货,则上传到货截图
     * 如果是异常,则修改异常状态并上传异常截图
     */
    @PutMapping("/updateOrderStatus")
    public Result updateOrderStatus(Integer id, Integer status, String transporterEvidence, Integer exceptionType){
        Order orderById = orderMapper.getOrderById(id);
        if(orderById.getStatus() != 2) {
            return Result.error("当前订单状态不是运输中，无法修改");
        }
        //如果是收货
        if(status == 3) {
            orderMapper.updateOrderStatus(null, id, status, transporterEvidence);
        }
        //如果是异常
        if(status == 5) {
            orderMapper.updateOrderStatus(exceptionType, id, status, transporterEvidence);
        }
        return Result.success("状态修改成功");
    }

    /**
     * 运输商接单， 将状态由待运输商接单（1）改为运输商运输中（2）
     */
    @PostMapping("takeOrder/{id}")
    public Result takeOrder(@PathVariable Integer id){
        String companyId = BaseContext.getCurrentId();//获取运输商手机号
        Order orderById = orderMapper.getOrderById(id);
        if(orderById.getStatus() != 1) {
            return Result.error("当前订单状态不是待运输中，无法接单");
        }
        orderMapper.takeOrder(id, companyId);
        return Result.success("接单成功");
    }

    /**
     *
     * @author yingfeng
     * @date 2024/4/2 18:40
     查询所有异常
     */
    @GetMapping("/getAllAbnormal")
    public Result<List<Abnormal>> getAllAbnormal(){
        List<Abnormal> allAbnormal = abnormalDao.getAllAbnormal();
        return Result.success(allAbnormal);
    }
}
